package com.example.tao.smartcar.view.valuecontrolview;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.RectF;
import android.os.Looper;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;

import com.example.tao.smartcar.R;

/**
 * Created by Tao on 2016/7/24.
 */
public class ValueControlView extends View {

    private ValueChangeListener listener = null;

    private static String TAG = "ValueControlView";

    private byte mValue = 0;
    private byte mOldValue = 0;
    private byte mMax = 40;
    private byte mMin = -40;

    private boolean mValueHold = false;
    private byte mPosSensitivity = 10;
    private byte mNegSensitivity = -10;

    private Paint panelPaint;
    private Paint valuePaint;
    private Paint valueTextPaint;
    private int valuePosition;

    private int viewHeight, viewWidth;
    private int valueHeight = 20;

    private RectF panelRectF, valueRectF;

    public ValueControlView(Context context) {
        this(context, null);
    }

    public ValueControlView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public ValueControlView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    private void init() {
        panelPaint = new Paint(Paint.ANTI_ALIAS_FLAG | Paint.DITHER_FLAG);
        panelPaint.setColor(getResources().getColor(R.color.white_20_alpha));
        panelPaint.setStyle(Paint.Style.FILL_AND_STROKE);

        valuePaint = new Paint(Paint.ANTI_ALIAS_FLAG | Paint.DITHER_FLAG);
        valuePaint.setColor(getResources().getColor(R.color.white_80_alpha));
        valuePaint.setStyle(Paint.Style.FILL_AND_STROKE);

        valueTextPaint = new Paint(Paint.ANTI_ALIAS_FLAG | Paint.DITHER_FLAG);
        valueTextPaint.setStyle(Paint.Style.FILL_AND_STROKE);
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        viewHeight = h;
        viewWidth = w;

        valuePosition = viewHeight/2;
        invalidateView();
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        drawBackground(canvas);
        drawPanel(canvas);
    }

    private void drawBackground(Canvas canvas) {
        canvas.drawColor(getResources().getColor(R.color.white_00_alpha));
    }

    private void drawPanel(Canvas canvas) {
        panelRectF = new RectF(0,0,viewWidth, viewHeight);
        canvas.drawRect(panelRectF, panelPaint);
        valueRectF = new RectF(0, valuePosition-valueHeight/2, viewWidth, valuePosition + valueHeight/2);
        canvas.drawRect(valueRectF, valuePaint);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {

        float x = event.getX();
        float y = event.getY();

        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                calculateValue(y);
                invalidateView();
                break;
            case MotionEvent.ACTION_MOVE:
                calculateValue(y);
                invalidateView();
                break;
            case MotionEvent.ACTION_UP:
                if (mValueHold) {
                    calculateValue(y);
                } else {
                    calculateValue(viewHeight/2);
                }
                invalidateView();
                break;
        }

        return true;
    }

    private void calculateValue(float y) {
        valuePosition = (int) y;
        //Log.d(TAG, "valuePosition:" + valuePosition);
        mValue = (byte)(mMax - ((float)valuePosition/(float)viewHeight)*(mMax - mMin));
        //Log.d(TAG, "mValue:" + mValue);

        if ((mValue < mPosSensitivity) && (mValue > mNegSensitivity)){
            mValue = 0;
            valuePosition = viewHeight/2;
        }

        if (mValue != mOldValue) {
            listener.onValueChanged(this);
        }
        mOldValue = mValue;
    }

    private void invalidateView() {
        if (Looper.getMainLooper() == Looper.myLooper()) {
            invalidate();
        } else {
            postInvalidate();
        }
    }

    public byte getValue(){
        return mValue;
    }

    public void setOnValueChangeListener(ValueChangeListener listener) {
        this.listener = listener;
    }

    public void setValueHold(boolean valueHold) {
        this.mValueHold = valueHold;
    }

    public void setPosSensitivity(byte posSensitivity) {
        this.mPosSensitivity = posSensitivity;
    }

    public void setNegSensitivity(byte negSensitivity) {
        this.mNegSensitivity = negSensitivity;
    }
}
